unit GenRest;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, FMTBcd, SqlExpr, DB;

type
  TGen = class(TDataModule)
    NewGen: TSQLDataSet;
    OldGen: TSQLDataSet;
    NewGenId: TSQLDataSet;
    OldGenId: TSQLDataSet;
  private
    { Private declarations }
    function Tbl(DBType: Byte): TDataSet;
    function TblId(DBType: Byte): TDataSet;
    function GenName(DBType : Byte) : String;
    function GenId(DBType : Byte) : Integer;

    procedure CreateGenerator;
    procedure SetGeneratorValue;
  public
    { Public declarations }
    procedure CheckGenerator;
  end;


procedure CheckGenerators(ProgBar : TProgressBar);

var
  Gen: TGen;

implementation

uses Data, ReportForm;

{$R *.DFM}

procedure CheckGenerators;
begin
  ProgBar.Position:=0;
  // how many generators are there ?
  Gen.NewGen.CommandText:=
    'SELECT count(RDB$GENERATOR_NAME) as ccc FROM RDB$GENERATORS '+
    'WHERE RDB$SYSTEM_FLAG<1 OR RDB$SYSTEM_FLAG IS NULL';
  Gen.NewGen.Open;
  ProgBar.Max:=Gen.NewGen.fieldByName('ccc').AsInteger;
  Gen.NewGen.Close;

  Gen.NewGen.CommandText:=
    'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS '+
    'WHERE RDB$SYSTEM_FLAG<1 OR RDB$SYSTEM_FLAG IS NULL';

  Gen.NewGen.Open;
  Gen.OldGen.Open;
  Gen.NewGen.First;
  While not Gen.NewGen.Eof do
  begin
    Application.ProcessMessages;
    ProgBar.Position:=ProgBar.Position+1;
    Gen.CheckGenerator;
    Gen.NewGen.Next;
  end;
  Gen.OldGen.Close;
  Gen.NewGen.Close;
end;

{ TDmn }

function TGen.Tbl(DBType: Byte): TDataSet;
begin
  Result:=Nil;
  case DBType of
    fcNew : Result:=NewGen;
    fcOld : Result:=OldGen;
  end;
end;

function TGen.TblId(DBType: Byte): TDataSet;
begin
  Result:=Nil;
  case DBType of
    fcNew : Result:=NewGenId;
    fcOld : Result:=OldGenId;
  end;
end;

function TGen.GenName(DBType: Byte): String;
begin
  Result:=Trim(Tbl(DBType).FieldByName('RDB$GENERATOR_NAME').AsString);
end;

function TGen.GenId(DBType: Byte): Integer;
begin
  With TSQLDataSet(TblId(DBType)) do
  begin
    CommandText:='SELECT GEN_ID('+GenName(DBType)+',0) FROM RDB$DATABASE';
    Open;
    Result:=FieldByName('GEN_ID').AsInteger;
    Close;
  end;
end;

procedure TGen.CreateGenerator;
begin
  DM.SQL.Clear;
  DM.SQL.Add('CREATE GENERATOR '+GenName(fcNew));

  if DM.AutoRepair then
  begin
    try
      DM.ExecRunSQL;
      if DM.ShowReport then Rep.AddToReport('GENERATOR',Trim(GenName(fcNew)),'not exist');
    except
      if DM.ShowReport then Rep.AddToReport('GENERATOR',Trim(GenName(fcNew)),'error');
    end;
  end else
  begin
    DM.AddToStrings('GENERATOR '+Trim(GenName(fcNew)),DM.SQL);
    if DM.ShowReport then Rep.AddToReport('GENERATOR',Trim(GenName(fcNew)),'not exist');
  end;
end;

procedure TGen.SetGeneratorValue;
begin
  DM.SQL.Clear;
  DM.SQL.Add('SET GENERATOR '+GenName(fcNew)+' TO '+IntToStr(GenId(fcNew)));

  if DM.AutoRepair then DM.ExecRunSQL else
    DM.AddToStrings('GENERATOR '+Trim(GenName(fcNew)),DM.SQL);

  if DM.ShowReport then Rep.AddToReport('GENERATOR',Trim(GenName(fcNew)),'value');
end;

procedure TGen.CheckGenerator;
begin
  OldGen.Close;
  OldGen.ParamByName('GName').AsString:=GenName(fcNew);
  OldGen.Open;
  if GenName(fcOld)<>GenName(fcNew) then
    CreateGenerator;
  // sometimes I have to check generator value
  try
    if GenId(fcNew)>GenId(fcOld) then
      SetGeneratorValue;
  except
  end;
end;
end.
